// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import Global from "../Global";
import StartUp from "../StartUp";
import { selfCls } from "../common/Decorator";
import { Manager } from "../common/Manager";
import Scene from "./Scene";

const {ccclass, property} = cc._decorator;

@selfCls
export default class SceneMgr extends Manager {
    get parentRoot(): cc.Node {
        return StartUp.instance.uiRoot;
    }

    // private static _instance:SceneMgr = null;
    // public static get instance() {
    //     if (SceneMgr._instance == null) {
    //         let node = new cc.Node();
    //         node.name = "SceneMgr";
    //         StartUp.instance.uiRoot.addChild(node);
    //         SceneMgr._instance = node.addComponent(SceneMgr);
    //         SceneMgr._instance.init();
    //     }
    //     return SceneMgr._instance;
        
    // }

    private _current:Scene = null;

    public async replaceScene<T extends Scene>(type: {new(): T;}) {
        let scene = new type();
        if (this._current != null) {
            this._current.onExit();
        }
        this._current = scene;
        await this._current.onEnter();
    }

    public getCurrentScene<T extends Scene>() {
        return this._current as T;
    }

    update(dt) {
        if (this._current == null) { return; }
        this._current.update(dt);
    }
}
